package com.kinggrid.mybatisplus.pagination.testcase;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kinggrid.mybatisplus.pagination.dto.UserDTO;
import com.kinggrid.mybatisplus.pagination.mapper.UserMapper;
import com.kinggrid.mybatisplus.pagination.pagination.Pagination;
import com.kinggrid.mybatisplus.pagination.utils.JsonUtils;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MapperTestCase extends ServiceImpl<UserMapper, UserDTO> {

    @Resource
    private UserMapper userMapper;

    @PostConstruct
    public void init() {
        //sql条件是预编译的
         testInsert1();
        // testinsert2();
        /*testfinbyAll1();
        testfinbyAll2();
        testfinbyId1(74);
        testfinbyConditon1();
      testUpdateByCondition1(75);
        testfinbyId1(75);*/

    }

    //=======以下的测试用例，sql都是预编译的==============
    public void testInsert1() {
        UserDTO userDTO = new UserDTO();
        userDTO.setAge(1758695069);
        userDTO.setEmail("238484@qq.com");
        userDTO.setIdcard("130722199102900995");
        userDTO.setIdcardSensitive("130722199102900995");
        userDTO.setUserName("张三丰");
        userDTO.setUserNameSensitive("张三丰");


        Map<String, Object> jsonMap = new HashMap<>();
        jsonMap.put("username", "张三丰");
        jsonMap.put("idcard", "13072219991947585896");
        jsonMap.put("age", 18);
        userDTO.setJsonStr(JsonUtils.parseMaptoJSONString(jsonMap));
        List list = new ArrayList();
        list.add(userDTO);
        int result = userMapper.insert(list);
        System.out.println(result);
    }

    //这种传值方式，加解密和脱敏无法生效，必须基于javaBean
    public void testinsert2() {
        int result = userMapper.insert2("马达哈", "12222");
        System.out.println(result);
    }


    public void testfinbyAll1() {

        List<UserDTO> userDTOS = userMapper.findAll();
        System.out.println(userDTOS);
    }

    //此种方式是基于resultmap查询
    public List<UserDTO> testfinbyAll2() {
        List<UserDTO> userDTOS = userMapper.findAll2();
        System.out.println(userDTOS);
        return userDTOS;
    }

    public UserDTO testfinbyId1(int id) {
        UserDTO userDTO = new UserDTO();
        userDTO.setId(id);
        UserDTO userDTO2 = userMapper.findOne(userDTO);
        System.out.println(userDTO2);
        return userDTO2;
    }

    public void testfinbyConditon1() {
        UserDTO userDTO = new UserDTO();
        userDTO.setIdcard("130722199102900995");
        List<UserDTO> userDTO2 = userMapper.findByCondition(userDTO);
        System.out.println(userDTO2);
    }

    public void testUpdateByCondition1(int id) {
        UserDTO userDTO = new UserDTO();
        userDTO.setId(id);

        UserDTO userDTO2 = userMapper.findOne(userDTO);
        userDTO2.setEmail("测试用email@qq.com");
        userDTO2.setIdcard("2344");
        userDTO2.setIdcardSensitive("2344");
        int result = userMapper.updateByCondition(userDTO2);
        System.out.println(result);
    }

    public Pagination list1() {
        Pagination<UserDTO> page = new Pagination<>();
        page.setCurrent(1);
        page.setSize(10);
        page = this.page(page);
        return page;
    }
}
